package com.example.issue.ui.issues

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.example.issue.R
import com.example.issue.network.ApiClient
import kotlinx.coroutines.launch

class IssuesActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_issues)

        val list = findViewById<ListView>(R.id.list)

        lifecycleScope.launch {
            try {
                val items = ApiClient.api.issues(null).map { "${'$'}{it.priority} · ${'$'}{it.title}" }
                list.adapter = ArrayAdapter(this@IssuesActivity, android.R.layout.simple_list_item_1, items)
            } catch (e: Exception) {
                list.adapter = ArrayAdapter(this@IssuesActivity, android.R.layout.simple_list_item_1, listOf("加载失败"))
            }
        }
    }
}
